package Library;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

/*4.Account_Service类负责提供账号登录、锁定账号的方法，对输入账号
或密码错误次数达到三次的用户进行锁定。*/

public class Account_Service {

	Scanner input = new Scanner(System.in);

	// Account login账号登录

	public int AccountLogin(ArrayList<register> registerList) throws InterruptedException {

		int localidentify = 0;// 返回是顾客还是商家

//账号和密码，放在外面判断账号和密码的时候都能用
		String account = null;
		String password = null;

		// 判断是否已经登陆

		// 从registerlist中获取当个register，访问内部account和password是否正确\
		int cnt = 0;// 判断输入次数是否超过三，若是则暂停30秒
		finishAcc: while (true) {
			if (cnt == 3) {
				System.out.println("您输入错误已达上次，请在三十秒后再输入");
				Thread.currentThread().sleep(1000);
				cnt = 0;
				continue finishAcc;
			}
			cnt++;
			System.out.println("请输入您的账号");
			account = input.nextLine();

			int regcnt = 1;
			for (register r : registerList) {
				// 若r.getAccount().equals(account)是true，则存在该账号
				if (r.getAccount().equals(account)) {
					regcnt = 100;
					break finishAcc;
				}
				// 通过list长度来判断是否已经遍历完
				if (regcnt == registerList.size()) {
					break;
				}
				regcnt++;
			}
			System.out.println("您输入的账号有误，请重新输入");

		}
//上面代码确保账号存在
		register localacc = null;

		for (register r : registerList) {
			if (r.getAccount().equals(account)) {
				localacc = r;
			}
		}

		int passcnt = 0;

		while (true) {
			System.out.println("请输入您的密码");
			password = input.nextLine();
			if (passcnt == 3) {
				System.out.println("密码错误已达三次，请过30秒后重新输入");
				Thread.currentThread().sleep(1000);
				continue;
			}
			if (localacc.getPassword().equals(password)) {
				System.out.println(account + "登陆成功");
				break;
			} else {
				System.out.println("您输入的密码有误，请重新输入");
				password = input.nextLine();
				continue;
			}

		}

//		//我要返回localidentify到main里面确认下一个界面
//		//匹配account是那个registerList里面并找出identify赋值给localidentify返回

		localidentify = localacc.getIdentify();
		return localidentify;

	}
}
